package org.myapp.common.bean;

import java.util.Collections;
import java.util.List;

/**
 * 分页基础类
 * 
 * @author Administrator
 *
 * @param <T>
 */
public class Page<T> {
	/** 最大的pageSize值,防止页面注入过大的值,造成程序响应慢 ,如果是接口调用,可以通过构造函数注入 **/
	public static final Integer MAX_PAGE_SIZE = 10000;

	private Integer pageNum = 1;

	private Integer pageSize = 10;

	private List<T> dataList = Collections.EMPTY_LIST;

	private Integer totalPage = 1;// 总页数

	public Page() {
	}

	public Page(Integer pageNum, Integer pageSize) {
		this.pageNum = pageNum;
		this.pageSize = pageSize;
	}

	public Page(Integer pageNum, Integer pageSize, List<T> dataList, Integer totalPage) {
		this.pageNum = pageNum;
		this.pageSize = pageSize;
		this.dataList = dataList;
		this.totalPage = totalPage;
	}

	public boolean hasPrevious() {
		return this.pageNum > 1 && this.pageNum <= totalPage;
	}

	public boolean hasNext() {
		return this.pageNum >= 1 && this.pageNum < totalPage;
	}

	public void setPageNum(Integer pageNum) {
		// 防止页面注入负数
		this.pageNum = pageNum < 1 ? 1 : pageNum;
	}

	public Integer getPageNum() {
		return pageNum;
	}

	public void setPageSize(Integer pageSize) {
		// TODO:日志警告
		// 防止页面输入过大的参数,响应时间过长,如果真想超过10000,请走构造函数
		this.pageSize = pageSize < 1 ? 1 : Math.min(MAX_PAGE_SIZE, pageSize);
	}

	public Integer getPageSize() {
		return pageSize;
	}

	public void setDataList(List<T> dataList) {
		// TODO:Null or Option
		this.dataList = dataList;
	}

	public List<T> getDataList() {
		return dataList;
	}
}
